﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.workPlan
{
    public partial class EmpSubmit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Maticsoft.BLL.workplan wm = new Maticsoft.BLL.workplan();
                if (!string.IsNullOrEmpty(Request["workplan_id"]))
                {
                    int workplan_id = int.Parse(Request["workplan_id"]);

                    Maticsoft.Model.workplan w = wm.GetModel(workplan_id);

                    this.txt_title.Text = w.workplan_title;
                    //this.txtncontent.Text = w.workplan_content;
                    this.ddl_type.SelectedValue = w.workplan_type;

                    this.Panel1.Visible = true;
                    this.btn_scjh.Visible = false;
                    this.btn_tjjh.Visible = true;

                    DataSet ds_wd = new Maticsoft.BLL.workplan_detail().GetList(" workplan_id= " + workplan_id + " order by orderId  ");

                    this.GridView1.DataSource = ds_wd;
                    this.GridView1.DataBind();

                }
            }
        }



        protected void btn_save_Click(object sender, EventArgs e)
        {
           
            if (Session["login"] != null)
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "提交计划成功！", "ListWorkPlan.aspx");
               
                //Maticsoft.BLL.workplan wpm = new Maticsoft.BLL.workplan();

                //if (!string.IsNullOrEmpty(Request["workplan_id"]))
                //{
                //    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "提交计划成功！", "ListWorkPlan.aspx");
                //}
                //else
                //{

                //    if (!string.IsNullOrEmpty(this.lbl_workplan_id.Text))
                //    {
                //        int workplan_id = int.Parse(this.lbl_workplan_id.Text);

                //        Maticsoft.Model.workplan w = wpm.GetModel(workplan_id);

                //        w.isSubmit = true;

                //        if (wpm.Update(w))
                //        {
                //            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "提交计划成功！", "ListWorkPlan.aspx");
                //        }
                //    }

                //}
               
            }
        }

        private void SetEntity(int roles_id, int user_id, Maticsoft.Model.workplan w)
        {
            if (roles_id == 1023)
            {
                //基层员工
                w.check_step = "班长审核";

            }
            if (roles_id == 1024)
            {
                //班长
                w.check_step = "绩效专责审核";

            }
            //if (roles_id == 1025)
            //{
            //    //绩效专责
            //    w.check_step = "绩效专责评价";

            //}

            w.isCheck = false;
            w.isSubmit = false;
            w.submit_time = DateTime.Now;
            w.submit_uid = user_id;
            //w.workplan_content = txtncontent.Text;
            w.workplan_title = txt_title.Text;
            w.workplan_type = this.ddl_type.SelectedValue;
        }

        //生成计划
        protected void btn_scjh_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                int user_depid = 0;
                if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["user_depid"].ToString()))
                {
                    user_depid = Convert.ToInt32(ds.Tables[0].Rows[0]["user_depid"].ToString());
                }

                Maticsoft.BLL.workplan wpm = new Maticsoft.BLL.workplan();

                if (!string.IsNullOrEmpty(Request["workplan_id"]))
                {

                }
                else
                {
                    Maticsoft.Model.workplan w = new Maticsoft.Model.workplan();


                    SetEntity(roles_id, user_id, w);

                    int workplan_id = wpm.Add(w);

                    if (workplan_id > 0)
                    {
                        Maticsoft.Common.MessageBox.Show(this, "生成计划成功！请填写计划内容");

                        this.btn_scjh.Visible = false;
                        this.btn_tjjh.Visible = true;
                        this.lbl_workplan_id.Text = workplan_id.ToString();
                        this.Panel1.Visible = true;
                    }
                }
            }
        }

        //添加计划内容
        protected void btn_tjjh_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.lbl_workplan_id.Text))
            {
                if (string.IsNullOrEmpty(this.txt_wcsj.Text))
                {
                    Maticsoft.Common.MessageBox.Show(this,"请填写完成时间！");
                    return;
                }

                int workplan_id = 0;

                workplan_id = Convert.ToInt32(this.lbl_workplan_id.Text);

                Maticsoft.BLL.workplan_detail wdm = new Maticsoft.BLL.workplan_detail();

                Maticsoft.Model.workplan_detail wd = new Maticsoft.Model.workplan_detail();

                wd.finishTime = DateTime.Parse(this.txt_wcsj.Text);
                wd.contentStr = this.txt_neirong.Text;
                wd.orderId = this.txt_xuhao.Text;
                wd.workplan_id = workplan_id;

                if (wdm.Add(wd) > 0)
                {
                    DataSet ds = wdm.GetList(" workplan_id= " + workplan_id + " order by orderId  ");

                    this.GridView1.DataSource = ds;
                    this.GridView1.DataBind();
                }

            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "请先生成工作计划！");
            }

            if (!string.IsNullOrEmpty(Request["workplan_id"]))
            {
                int workplan_id = 0;

                workplan_id = Convert.ToInt32(Request["workplan_id"]);

                Maticsoft.BLL.workplan_detail wdm = new Maticsoft.BLL.workplan_detail();

                Maticsoft.Model.workplan_detail wd = new Maticsoft.Model.workplan_detail();

                wd.finishTime = DateTime.Parse(this.txt_wcsj.Text);
                wd.contentStr = this.txt_neirong.Text;
                wd.orderId = this.txt_xuhao.Text;
                wd.workplan_id = workplan_id;

                if (wdm.Add(wd) > 0)
                {
                    DataSet ds = wdm.GetList(" workplan_id= " + workplan_id + " order by orderId  ");

                    this.GridView1.DataSource = ds;
                    this.GridView1.DataBind();
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_del")
            {
                int id = int.Parse(e.CommandArgument.ToString());

                Maticsoft.BLL.workplan_detail wdm = new Maticsoft.BLL.workplan_detail();
                
                if (wdm.Delete(id))
                {
                    int workplan_id = 0;

                    if (!string.IsNullOrEmpty(this.lbl_workplan_id.Text))
                    {
                        workplan_id = int.Parse(this.lbl_workplan_id.Text);
                    }
                    if (!string.IsNullOrEmpty(Request["workplan_id"]))
                    {
                        workplan_id = int.Parse(Request["workplan_id"]);
                    }

                    DataSet ds = wdm.GetList(" workplan_id= " + workplan_id + " order by orderId  ");

                    this.GridView1.DataSource = ds;
                    this.GridView1.DataBind();
                }
            }
        }


      
    }
}